//
//  WXPPaymenCodeView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/19.
//

#import "WXPPaymenCodeView.h"

@implementation WXPPaymenCodeView

#pragma mark - lazy
- (UIImageView *)backView{
    if (_backView == nil) {
        _backView = [UIImageView new];
        _backView.userInteractionEnabled = true;
        _backView.image = [UIImage imageNamed:@"icon-stp-bg"];
        _backView.widthHeight = XY(SCREEN_WIDTH-4*LEFT_MARDIN, (SCREEN_WIDTH-4*LEFT_MARDIN)/313*60);
    }
    return _backView;
}
- (UIImageView *)imageContentBack{
    if (!_imageContentBack) {
        _imageContentBack = [UIImageView new];
        _imageContentBack.backgroundColor = [UIColor clearColor];
        _imageContentBack.image = [UIImage imageNamed:@"icon-repaymentBank-tag"];
    }
    return _imageContentBack;
}
- (UILabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [UILabel new];
        _contentLabel.textColor = COLOR_WHITE;
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.fontNum = (14);
    }
    return _contentLabel;
}
- (UILabel *)numberLabel{
    if (_numberLabel == nil) {
        _numberLabel = [UILabel new];
        _numberLabel.textColor = COLOR_BLACK;
        _numberLabel.font = [UIFont boldSystemFontOfSize:(14)];
    }
    return _numberLabel;
}
- (UIButton *)fuzhiBtn{
    if (_fuzhiBtn == nil) {
        _fuzhiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _fuzhiBtn.tag = 1;
        [_fuzhiBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _fuzhiBtn.widthHeight = XY(100, Ratio(30));
        [GlobalMethod setRoundView:_fuzhiBtn color:[UIColor clearColor] numRound:_fuzhiBtn.height/2 width:0];
        _fuzhiBtn.backgroundColor = COLOR_PRIMARY;
        _fuzhiBtn.titleLabel.fontNum = 14;
        [_fuzhiBtn setTitle:@"Copy" forState:UIControlStateNormal];
        [_fuzhiBtn setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
    }
    return _fuzhiBtn;
}
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH-LEFT_MARDIN*2;
        [self addSubView];
    }
    return self;
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = self.numberLabel.text;
            [GlobalMethod showAlert:@"Copia éxito"];
        }
            break;
              
        default:
            break;
    }
}

//添加subview
- (void)addSubView{
    [self addSubview:self.backView];
    [self addSubview:self.imageContentBack];
    [self addSubview:self.contentLabel];
    [self.backView addSubview:self.numberLabel];
    [self.backView addSubview:self.fuzhiBtn];
    
    //初始化页面
    [self resetViewWithModel:nil];
}

#pragma mark - 刷新view
- (void)resetViewWithModel:(ModelRepaymentCodeList *)model{
    self.contentLabel.hidden = model.tag != 1;
    self.imageContentBack.hidden = self.contentLabel.hidden;
    [self.contentLabel fitTitle:readStr(model.repaymentBank) variable:0];
    self.imageContentBack.widthHeight =  XY(MAX(self.contentLabel.width+LEFT_MARDIN*2, 75), 26);
    self.imageContentBack.leftBottom = XY((26), 26);
    self.contentLabel.centerXCenterY = XY(self.imageContentBack.centerX, self.imageContentBack.centerY-2);
    
    self.backView.centerXTop = XY(self.width/2, self.imageContentBack.top+12);
    [GlobalMethod setRoundView:self.backView color:[UIColor clearColor] numRound:10 width:0];
    
    self.fuzhiBtn.rightCenterY = XY(self.backView.width-LEFT_MARDIN, self.backView.height/2);
    
    [self.numberLabel fitTitle:readStr(model.paymentCode) variable:0];
    self.numberLabel.leftCenterY = XY((26), self.fuzhiBtn.centerY);
    
    //设置总高度
    self.height = self.backView.bottom;
}

@end
